package com.feng.study.controller;

import com.feng.study.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Zeng ShaoYong
 * @Date: 2021-11-04
 * @Description:
 */
@RestController
@Slf4j
public class AsyncDemo {
    @Autowired
    private AsyncService asyncService;

    @GetMapping("/async/demo")
    public String test() throws InterruptedException {
        try{
            log.info("异步调用开始...");
            System.out.println("111.....");
            asyncService.asyncVoid();
            System.out.println("11222.....");
            log.info("异步调用结束...");
        }catch (Exception e){
            //异步方法抛异常，也不会走到这里捕捉异常，打印日志都无效
            //因此error日志应该在异步方法里面打印
            log.error("异步调用异常");
            System.out.println("异步调用异常");
            return "error";
        }
//        Thread.sleep(12000);
        return "success";
    }

    @GetMapping("/async/demo01")
    public void test001(){
        test002();
    }

    @Async
    public void test002(){
        System.out.println("异步");
    }
}
